import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/app_constant.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/home/model/common_tag_model.dart';
import 'package:tender_flutter/page/supplier/model/supplier_params_entity.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/router/router_map.gr.dart';
import 'package:tender_flutter/utils/picker.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/form_widget/form_option_item.dart';

import 'api/api_supplier.dart';

class SupplierPersonalFormPage extends StatefulWidget {
  @override
  _SupplierPersonalFormPageState createState() =>
      _SupplierPersonalFormPageState();
}

class _SupplierPersonalFormPageState extends State<SupplierPersonalFormPage>
    with AutomaticKeepAliveClientMixin {
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  /// 行业数据
  List<CommonTagModel> _industryDatas;

  /// 表单参数
  /// type: 个人供应商
  SupplierParamsEntity _paramsEntity =
      SupplierParamsEntity(type: AppConstant.SUPPLIER_PERSION);

  void _forSubmitted() async {
    LRouter.navigatorCtx(context).push(Routes.supplierCertificateListPage,
        arguments:
            SupplierCertificateListPageArguments(paramsEntity: _paramsEntity));
    // var _form = _formKey.currentState;
    // if (_form.validate()) {
    //   _form.save();
    //   // print(_username);
    //   // print(_password);
    // }
  }

  /// 获取行业类别
  Future _getIndustryCategory() async {
    if (_industryDatas != null && _industryDatas.isNotEmpty) {
      return _industryDatas;
    }
    var response =
        await ApiSupplier.getSupplierIndustryCategory<CommonListTagModel>();
    _industryDatas = response.result.xList;
    return _industryDatas;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return _initFormData();
  }

  /// 个人基本信息
  Widget _initFormData() {
    return SingleChildScrollView(
        child: Form(
            key: _formKey,
            child: Column(children: <Widget>[
              FormOptionItem(
                  title: "姓名",
                  placeholder: "请输入姓名",
                  content: _paramsEntity.contact ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    _paramsEntity.contact = value;
                  }),
              FormOptionItem(
                  title: "联系电话",
                  placeholder: "请输入联系电话",
                  content: _paramsEntity.phone ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    _paramsEntity.phone = value;
                  }),
              FormOptionItem(
                  title: "专业分类",
                  placeholder: "请选择专业分类",
                  content: _paramsEntity.labelStr ?? "",
                  selectCallback: () {
                    _getIndustryCategory().then((value) => {
                          PickerUtils.showStringPicker(context,
                              data: _industryDatas.map((e) => e.title).toList(),
                              clickCallBack: (index, val) {
                            setState(() {
                              _paramsEntity.labelStr =
                                  _industryDatas[index].title;
                              _paramsEntity.labels = [
                                {"id": _industryDatas[index].id}
                              ];
                            });
                          })
                        });
                  }),
              FormOptionItem(
                  title: "所在企业",
                  placeholder: "请输入企业名称",
                  content: _paramsEntity.companyName,
                  inputOnly: true,
                  onChangeCallback: (value) {
                    _paramsEntity.companyName = value;
                    _paramsEntity.title = value;
                  }),
              FormOptionItem(
                  title: "所在地区",
                  placeholder: "请选择所在区域",
                  content: _paramsEntity.city ?? "",
                  selectCallback: () {
                    PickerUtils.showCityPicker(context,
                        clickCallBack: (codes, value) {
                      setState(() {
                        _paramsEntity.areaId = codes.last;
                        _paramsEntity.city = value;
                      });
                    });
                  }),
              FormOptionItem(
                  title: "详细地址",
                  placeholder: "请输入详细地址",
                  content: _paramsEntity.address,
                  inputOnly: true,
                  onChangeCallback: (value) {
                    _paramsEntity.address = value;
                  }),
              FormOptionItem(
                  title: "微信",
                  placeholder: "请输入微信",
                  content: _paramsEntity.wxAccount,
                  inputOnly: true,
                  onChangeCallback: (value) {
                    _paramsEntity.wxAccount = value;
                  }),
              _getSubmitBtn()
            ])));
  }

  /// 提交按钮
  Widget _getSubmitBtn() {
    return Container(
        height: ScreenAdapter.height(90),
        width: double.infinity,
        margin: EdgeInsets.symmetric(
            vertical: ScreenAdapter.height(50),
            horizontal: ScreenAdapter.width(30)),
        decoration: BoxDecoration(
            color: ThemeColors.colorType6,
            borderRadius: BorderRadius.circular(ScreenAdapter.width(14))),
        child: FlatButton(
            onPressed: () {
              _forSubmitted();
            },
            child: Text("确定",
                style: TextStyle(
                    color: ThemeColors.colorWhite,
                    fontSize: ScreenAdapter.sp(32)))));
  }
}
